package org.fuys.etp.core.pool.algorithm;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @projectName: fuys-etp-final
 * @package: org.fuys.etp.core.pool.algorithm
 * @className: TaskCountPredictor
 * @author: WangJing
 * @description: 任务数量预测器
 * @date: 2025/2/10 21:20
 * @version: 1.0
 */
public class TaskCountPredictor {
    //窗口保存
    private final Deque<Double> windows = new ArrayDeque<>();
    //窗口大小
    private final int WINDOW_MAX_SIZE;
    //预测时窗口最低阈值
    private final int WINDOW_MIN_SIZE;
    //窗口权重
    private final double[] weights;

    public TaskCountPredictor(int windowSize,int windowMinSize ,double[] weights){
        this.WINDOW_MAX_SIZE = windowSize;
        this.WINDOW_MIN_SIZE = windowMinSize;
        this.weights = weights;
    }

    public void record(double taskCount){
        if(windows.size() == WINDOW_MAX_SIZE){
            windows.removeFirst();
        }
        windows.addLast(taskCount);
    }

    public double predict(){
        if(windows.size()<WINDOW_MIN_SIZE){
            return 0;
        }
        double prediction = 0;
        int i = 0;
        for (Double window : windows) {
            prediction += weights[i++] * window;
        }
        return prediction;
    }
}
